package cn.tjd.smart_health.admin.controller;


import cn.tjd.smart_health.api.OrderSetting;
import cn.tjd.smart_health.common.constant.MessageConstant;
import cn.tjd.smart_health.common.entity.ReserveSetting;
import cn.tjd.smart_health.common.util.ExcelUtil;
import cn.tjd.smart_health.common.util.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @DubboReference
    private OrderSetting orderSettingService;


    @PostMapping("/upload")
    public R upload(@RequestParam("excelFile") MultipartFile fileData){

        if (fileData == null) {
            return new R(false, "excel文件处理失败");
        }
        try {
            byte[] bytes = fileData.getBytes();
            String fileName = fileData.getOriginalFilename();
            fileName= fileName;
            File file = new File(fileName);
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(bytes);
            List<ReserveSetting> reserveSettingList = ExcelUtil.getInstance(file);
            orderSettingService.saveOrderSettingList(reserveSettingList);

            return new R(true, "excel文件处理成功", reserveSettingList);
        } catch (IOException e) {
            e.printStackTrace();
            return new R(false,"excel文件处理失败");
        }

    }

    @PostMapping("/getOrderSettingByMonth")
    public R getReserveSettingList(@Param("date") String date){

        try {
            String year = date.split("-")[0];
            String month = date.split("-")[1];
            List<ReserveSetting> list = orderSettingService.getOrderSettingList(year,month);
            return new R(true, "获取成功", list);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, "获取失败");
        }

    }

    @PostMapping("/editNumberByDate")
    public R editNumber(@RequestBody ReserveSetting reservationSetting){
        try {
            orderSettingService.saveReserveSetting(reservationSetting);
            return new R(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

}
